package controller;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import service.MemberService;
import service.MessageManagerService;
import dto.MemberDTO;
import dto.MessageDTO;


@Controller
public class MessageController {
	private MessageManagerService service;
	private MemberService mService;
	public MessageController(MemberService mService) {
		this.mService = mService;
	}
	public MessageController(MessageManagerService service) {
		this.service = service;
	}
	
	@RequestMapping(value="goMessageInput.do", method=RequestMethod.GET)
	public ModelAndView goToMessageForm(HttpServletRequest request) {
		ModelAndView mv = null;
		
		try {
			HashMap map = new HashMap();
			int room_no = Integer.parseInt(request.getParameter("room_no"));
			MemberDTO mto = service.getMemberInfoByRoomNo(room_no);
			String reader = mto.getLast_name()+mto.getFirst_name();
			map.put("reader", reader);
			map.put("room_no", room_no);
			mv = new ModelAndView("message_input.jsp", "map", map);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping(value="writeMessage.do")
	public ModelAndView writeMessage(HttpServletRequest request, HttpSession session, MessageDTO mto) {
		//수신자 이름(방주인) - hidden을 통해 value를 넘김
		mto.setreader(request.getParameter("reader"));
		mto.setroomNo(Integer.parseInt(request.getParameter("room_no")));
		//메세지 쓰기
		service.registerMessage(mto);
		//로그인한 사용자의 정보 조회
		MemberDTO mdto = (MemberDTO) session.getAttribute("login_info");
		String writer= mdto.getLast_name()+mdto.getFirst_name();
		HashMap map = new HashMap();
		map.put("writer", writer);
		map.put("mto", mto);
		return new ModelAndView("message_success.message", "map", map);
	}
	
	@RequestMapping(value="reWriteMessage.do")
	public ModelAndView reWriteMessage(HttpServletRequest request, HttpSession session, MessageDTO mto) {
		//수신자 이름(방주인) - hidden을 통해 value를 넘김
		ModelAndView mv = null;
		try {
		mto.setreader(request.getParameter("reader"));
		mto.setroomNo(Integer.parseInt(request.getParameter("room_no")));
		mto.setWriter(request.getParameter("writer"));
		mto.setMsg_content(mto.getMsg_content());
		//메세지 쓰기
		service.registerMessage(mto);
		//로그인한 사용자의 정보 조회
		MemberDTO mdto = (MemberDTO) session.getAttribute("login_info");
		String writer= mdto.getLast_name()+mdto.getFirst_name();
		HashMap map = new HashMap();
		map.put("writer", writer);
		map.put("mto", mto);
		mv = new ModelAndView("message_success.message", "map", map);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping(value="getAllMessageInfo.do", method=RequestMethod.GET)
	public ModelAndView getMessageList(HttpSession session) {
		ArrayList<MessageDTO> list;
		ModelAndView mv = null;
		try {
			HashMap map = new HashMap();
			ArrayList resList = new ArrayList();
			MemberDTO mto =(MemberDTO) session.getAttribute("login_info");
			map.put("writer", mto.getMember_id());
			map.put("reader", mto.getLast_name() + mto.getFirst_name());
			list = service.getAllMessageList(map);
			for(MessageDTO m : list) {
				HashMap map1 = new HashMap();
				map1.put("mto", m);
				String writerName = service.getWriterNameByMemberId(m.getWriter()).getLast_name()+service.getWriterNameByMemberId(m.getWriter()).getFirst_name();
				map1.put("writerName", writerName);
				resList.add(map1);
			}
			mv = new ModelAndView("message_list.message", "resList", resList);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}

	@RequestMapping(value="getWriterMessage.do")
	public ModelAndView getWriterMessageInfo(HttpSession session) {
		ModelAndView mv = null;
		ArrayList <MessageDTO> list;
		ArrayList resList = new ArrayList();
		MemberDTO mto = (MemberDTO) session.getAttribute("login_info");
		HashMap map1 = new HashMap();
		try {
			String name = mto.getLast_name() + mto.getFirst_name();
			list = service.getWriterMessageById(mto.getMember_id());
			for(MessageDTO m : list) {
				HashMap map = new HashMap();
				map.put("mdto", m); 
				map.put("writer", name);
				map1.put("last_name", m.getreader().substring(0, 1));
				map1.put("first_name", m.getreader().substring(1, 3));
				MemberDTO nameDTO = service.getMemberIdByName(map1);
				System.out.println(nameDTO);
				map.put("reader_id", nameDTO.getMember_id());
				resList.add(map);
			}
//			map.put("list", list);
//			map.put("writer", name);
			mv = new ModelAndView("writerMessage_List.message", "resList", resList);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping(value="getReaderMessage.do")
	public ModelAndView getReaderMessageInfo(HttpSession session) {
		ModelAndView mv = null;
		ArrayList<MessageDTO> list;
		ArrayList resList = new ArrayList();
		MemberDTO mto = (MemberDTO) session.getAttribute("login_info");
		try {
			String name = mto.getLast_name() + mto.getFirst_name();
			list = service.getReaderMessageById(name);
			for(MessageDTO mdto : list){
				HashMap map = new HashMap();
				map.put("mdto", mdto);
				MemberDTO dto = service.getWriterNameByMemberId(mdto.getWriter());
				String writerName = dto.getLast_name()+dto.getFirst_name();
				map.put("name", writerName);
				resList.add(map);
			}
			mv = new ModelAndView("readerMessage_List.message", "resList", resList);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping(value="getMessageRelationWAR.do")
	public ModelAndView getMessageRelationWriterAndReader(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = null;
		ArrayList resList = new ArrayList();
		MemberDTO mto = (MemberDTO) session.getAttribute("login_info");
		try {
			HashMap map = new HashMap();
			map.put("reader_id", mto.getMember_id());
			map.put("reader_name", request.getParameter("reader"));
			map.put("writer_id", request.getParameter("writer_id"));
			map.put("writer_name", request.getParameter("writer_name"));
			ArrayList<MessageDTO> list = service.getSpecficMessageWAR(map);
			for(MessageDTO m : list) {
				HashMap map1 = new HashMap();
				map1.put("mdto", m);
				String writerName = service.getWriterNameByMemberId(m.getWriter()).getLast_name()+service.getWriterNameByMemberId(m.getWriter()).getFirst_name();
				map1.put("writerName", writerName);
				resList.add(map1);
			}
			mv = new ModelAndView("messageRelation_war.message", "resList", resList);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping(value="getMessageRelationWAR2.do")
	public ModelAndView getMessageRelationWriterAndReader2(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = null;
		ArrayList resList = new ArrayList();
		MemberDTO mto = (MemberDTO) session.getAttribute("login_info");
		try {
			HashMap map = new HashMap();
			map.put("reader_id", request.getParameter("reader"));
			map.put("reader_name", request.getParameter("reader_name"));
			map.put("writer_id", mto.getMember_id());
			map.put("writer_name", request.getParameter("writer_name"));
			ArrayList<MessageDTO> list = service.getSpecficMessageWAR(map);
			for(MessageDTO m : list) {
				HashMap map1 = new HashMap();
				map1.put("mdto", m);
				String writerName = service.getWriterNameByMemberId(m.getWriter()).getLast_name()+service.getWriterNameByMemberId(m.getWriter()).getFirst_name();
				map1.put("writerName", writerName);
				resList.add(map1);
			}
			mv = new ModelAndView("messageRelation_war2.message", "resList", resList);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping(value="goMessageInput2.do")
	public ModelAndView goToMessageForm2(HttpServletRequest request, HttpSession session) {
		ModelAndView mv = null;
		try {
			HashMap map = new HashMap();
			int room_no = Integer.parseInt(request.getParameter("room_no"));
			String writer_name = request.getParameter("writer_name");
			MemberDTO mdto = (MemberDTO) session.getAttribute("login_info");
			//홍명보
			String writer= mdto.getLast_name()+mdto.getFirst_name();
			MemberDTO mto = service.getMemberInfoByRoomNo(room_no);
			String reader = mto.getLast_name()+mto.getFirst_name();
			if(writer.equals(reader)) {
				reader = writer_name;
			} else {
				reader = mto.getLast_name()+mto.getFirst_name();
			}
			map.put("reader", reader);
			map.put("room_no", room_no);
			mv = new ModelAndView("message_input2.jsp", "map", map);
		} catch(Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.res", "error_message", e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping(value="reInput.do")
	public ModelAndView reInput(HttpServletRequest request, HttpSession session, MessageDTO mto) {
		mto.setreader(request.getParameter("reader"));
		mto.setroomNo(Integer.parseInt(request.getParameter("room_no")));
		mto.setWriter(request.getParameter("writer"));
		//메세지 쓰기
		service.registerMessage(mto);
		//로그인한 사용자의 정보 조회
		MemberDTO mdto = (MemberDTO) session.getAttribute("login_info");
		String writer= mdto.getLast_name()+mdto.getFirst_name();
		HashMap map = new HashMap();
		map.put("writer", writer);
		map.put("mto", mto);
		return new ModelAndView("message_success.message", "map", map);
	}
	
}
